package pres.wchen.oa.cor.shiro.web.wx;

import com.alibaba.fastjson.JSON;
import net.sf.json.JSONObject;
import org.jeewx.api.core.common.WxstoreUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @作者： wchen
 * @描述：
 * @创建时间： 2018/1/29 15:05
 * @版本：Copyright 2018, Inc. All Rights Reserved.
 * @修改信息：
 */
//@RestController
//@RequestMapping("/wx")
public class WxController {


//    @GetMapping("/addMemberCard")
    public static void createMemberCard(){
        Map cardMap = new HashMap();
        cardMap.put("card_type", "MEMBER_CARD"); //会员卡类型

        Map memberCardMap = new HashMap();
        memberCardMap.put("background_pic_url", "http://mmbiz.qpic.cn/mmbiz_jpg/nTliaicJzLdWz4log0jfV2XETdSaWN5pgkxqAlPhhLWOLLJ1Tm9ibUPjibzzy8iauJAXPLdlUj55L0BCdibj3szOgUVA/0"); //商家自定义会员卡背景图,需先上传
        memberCardMap.put("prerogative", "test_prerogative"); //会员卡特权说明
        memberCardMap.put("supply_bonus", false); //显示积分
        memberCardMap.put("supply_balance", false); //是否支持储值
        memberCardMap.put("auto_activate", false); //是否自动激活
        memberCardMap.put("activate_url", "http://www.qq.com"); //激活跳转的url

        Map customCellMap = new HashMap();
        customCellMap.put("name", "使用入口2"); //入口名称
        customCellMap.put("tips", "激活后显示"); //入口右侧提示语
        customCellMap.put("url", "http://www.xxx.com"); //入口跳转链接
        memberCardMap.put("custom_cell1", customCellMap); //结构

        Map baseInfoMap = new HashMap();
        baseInfoMap.put("logo_url", "http://115.159.75.46:8888/group1/M00/00/00/rBEACVnXMPqAFNHJAABaU2i8t-M161.png"); //卡券的商户logo
        baseInfoMap.put("code_type", "CODE_TYPE_QRCODE"); //展示类型CODE_TYPE_QRCODE 二维码
        baseInfoMap.put("brand_name", "海底捞"); //商户名字
        baseInfoMap.put("title", "海底捞会员卡"); //卡券名
        baseInfoMap.put("color", "Color010"); //券颜色
        baseInfoMap.put("notice", "使用时向服务员出示此券"); //卡券使用提醒
        baseInfoMap.put("description", "不可与其他优惠同享"); //卡券使用说明
        Map skuMap = new HashMap();
        skuMap.put("quantity", 50000000);//卡券库存的数量
        baseInfoMap.put("sku", skuMap); //商品信息
        Map dateInfoMap = new HashMap(); //使用日期
        dateInfoMap.put("type", "DATE_TYPE_PERMANENT");//卡券库存的数量
        baseInfoMap.put("date_info", dateInfoMap);

        memberCardMap.put("base_info", baseInfoMap); //基本的卡券数据

        cardMap.put("member_card", memberCardMap);

        Map dataMap = new HashMap();
        dataMap.put("card", cardMap);


//        JSONObject jsonObject = WxUtil.httpRequestWx(WxUtil.CREATE_CARD_URL, WxUtil.token, JSON.toJSONString(dataMap));
        String url = WxUtil.CREATE_CARD_URL;
        String requestUrl = url.replace("ACCESS_TOKEN", WxUtil.token);
//        JSONObject param = JSONObject.fromObject(paramObj);
        System.out.println("************************************");
        System.out.println(JSON.toJSONString(dataMap));
        System.out.println("************************************");
        System.out.println("************************************");
        System.out.println("************************************");

        JSONObject result = WxstoreUtils.httpRequest(requestUrl, "POST", JSON.toJSONString(dataMap));


        System.out.println(result.toString());


    }



    public static void getMemberCard(){
        //p8q1v0uYBCT4cOXOELSxO2e3HE3g

        Map cardMap = new HashMap();
        cardMap.put("membership_number", "123456789");
        cardMap.put("code", "123456789");
        cardMap.put("card_id", "p8q1v0uYBCT4cOXOELSxO2e3HE3g");

        String url = "https://api.weixin.qq.com/card/membercard/activate?access_token=TOKEN";
        String requestUrl = url.replace("TOKEN", WxUtil.token);
//        JSONObject param = JSONObject.fromObject(paramObj);
        System.out.println("************************************");
        System.out.println(JSON.toJSONString(cardMap));
        System.out.println("************************************");
        System.out.println("************************************");
        System.out.println("************************************");

        JSONObject result = WxstoreUtils.httpRequest(requestUrl, "POST", JSON.toJSONString(cardMap));


        System.out.println(result.toString());
    }


    public static void deleteMemberCard(){
        String url = "https://api.weixin.qq.com/card/delete?access_token={0}";
        String requestUrl = url.replace("{0}", WxUtil.token);
        Map dataMap = new HashMap();
        dataMap.put("card_id", "pcQqj1WPJDGCCj4SopUZ1irJ5WEA");
        JSONObject result = WxstoreUtils.httpRequest(requestUrl, "POST", JSON.toJSONString(dataMap));
        System.out.println(result.toString());

    }

    public static void getJsapiTicket(){

        String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi";
        String requestUrl = url.replace("ACCESS_TOKEN", WxUtil.token);
        JSONObject result = WxstoreUtils.httpRequest(requestUrl, "GET", null);
        System.out.println(result.toString());
    }



    public static void main(String[] args) {
//        getMemberCard();
//        createMemberCard();
        deleteMemberCard();
    }
}
